package com.esa.doxml;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.xml.sax.SAXException;

import com.esa.javabean.Student;

public class CreateDetailXML{
	private OutPutXML opx=null;
	public File createFile(List<Student> list) throws IOException, SAXException{
		
		opx=new OutPutXML();
		opx.setFilename("C:\\Users\\LiYan\\Desktop\\xml\\C\\std\\std.xml");
		
		Element root = DocumentHelper.createElement("students");
		Element from=root.addElement("from");
		from.setText("C");
		Element to=root.addElement("to");
		String str=list.get(0).getSno();
		char cha=str.charAt(0);
		if(cha=='2'){
			to.setText("B");
		}else if(cha=='1'){
			to.setText("A");
		}else{
			System.out.println("CreateChoiceXML line 36...");
		}
		Element order=root.addElement("order");
		order.setText("detail");
		  
		Document document = DocumentHelper.createDocument();
		document.setRootElement(root);
		  for (Student std : list) {
		   root.add(getXML(std));
		  }
		  
		return opx.outPut(root);  
	}
	private Element getXML(Student student){
		Document doc=DocumentHelper.createDocument();
		Element root=doc.addElement("student");
		
		Element sno=root.addElement("Sno");
		sno.setText(student.getSno());
		
		Element sname=root.addElement("Snm");
		sname.setText(student.getSname());
		
		Element sex=root.addElement("Sex");
		sex.setText(student.getSex());
		
		Element ins=root.addElement("Sde");
		ins.setText(student.getInsititute());
		
		return root;
	}
}
